package client

import (
	course "0519/common/proto"
	"context"
	"google.golang.org/grpc"
)

type Handler func(ctx context.Context, in course.CourseClient) (interface{}, error)

func CourseClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8899", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := course.NewCourseClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	defer dial.Close()
	return res, nil
}

func UserLogin(ctx context.Context, i *course.UserLoginReq) (*course.UserLoginResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.UserLogin(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.UserLoginResp), nil
}

func CourseAdd(ctx context.Context, i *course.CourseAddReq) (*course.CourseAddResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.CourseAdd(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.CourseAddResp), nil
}

func CourseList(ctx context.Context, i *course.CourseListReq) (*course.CourseListResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.CourseList(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.CourseListResp), nil
}

func CourseInfo(ctx context.Context, i *course.CourseInfoReq) (*course.CourseInfoResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.CourseInfo(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.CourseInfoResp), nil
}

func CourseUpdateState(ctx context.Context, i *course.CourseUpdateStateReq) (*course.CourseUpdateStateResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.CourseUpdateState(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.CourseUpdateStateResp), nil
}
func CourseDeleted(ctx context.Context, i *course.CourseDeletedReq) (*course.CourseDeletedResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.CourseDeleted(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.CourseDeletedResp), nil
}

func CreateDiscountCourse(ctx context.Context, i *course.CreateDiscountCourseReq) (*course.CreateDiscountCourseResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.CreateDiscountCourse(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.CreateDiscountCourseResp), nil
}
func CreateDiscountCourseOrder(ctx context.Context, i *course.CreateDiscountCourseOrderReq) (*course.CreateDiscountCourseOrderResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.CreateDiscountCourseOrder(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.CreateDiscountCourseOrderResp), nil
}
func OrderList(ctx context.Context, i *course.OrderListReq) (*course.OrderListResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.OrderList(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.OrderListResp), nil
}
func OrderPay(ctx context.Context, i *course.OrderPayReq) (*course.OrderPayResp, error) {
	client, err := CourseClient(ctx, func(ctx context.Context, in course.CourseClient) (interface{}, error) {
		add, err := in.OrderPay(ctx, i)
		if err != nil {
			return nil, err
		}
		return add, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*course.OrderPayResp), nil
}
